//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package com.lx.composetest.adapter;

import android.view.MotionEvent;

import java.util.ArrayList;
import java.util.List;

/**
 * 贝塞尔曲线
 */
public class MotionEventBezier {
    private static final String TAG = MotionEventBezier.class.getSimpleName();
    MotionEvent preEvent1 = null;
    MotionEvent preEvent2 = null;
    public long downTime = 0L;

    public MotionEventBezier() {
    }

    public List<MotionEvent> process(MotionEvent event) {
        if (event.getAction() != MotionEvent.ACTION_DOWN && event.getAction() != MotionEvent.ACTION_MOVE) {
            List<MotionEvent> out = new ArrayList();
            if (this.preEvent1 != null) {
                List<MotionEvent> res = this.bezier(this.preEvent2);
                out.addAll(res);
            }

            this.preEvent1 = null;
            this.preEvent2 = null;
            out.add(event);
            return out;
        } else {
            if (event.getAction() == MotionEvent.ACTION_DOWN) {
                this.downTime = System.currentTimeMillis();
            }

            if (this.preEvent1 == null) {
                this.preEvent1 = MotionEvent.obtain(event);
                this.preEvent2 = MotionEvent.obtain(event);
            }

            List<MotionEvent> res = this.bezier(event);
            this.preEvent1 = this.preEvent2;
            this.preEvent2 = MotionEvent.obtain(event);
            return res;
        }
    }

    private List<MotionEvent> bezier(MotionEvent event) {
        float mx1 = (this.preEvent1.getX() + this.preEvent2.getX()) / 2.0F;
        float my1 = (this.preEvent1.getY() + this.preEvent2.getY()) / 2.0F;
        float mp1 = (this.preEvent1.getPressure() + this.preEvent2.getPressure()) / 2.0F;
        float mx2 = (event.getX() + this.preEvent2.getX()) / 2.0F;
        float my2 = (event.getY() + this.preEvent2.getY()) / 2.0F;
        float mp2 = (event.getPressure() + this.preEvent2.getPressure()) / 2.0F;
        float dis = distrance(mx1, my1, mx2, my2);
        int len = 1;
        if (dis > 1.0F) {
            len = (int)(dis / 2.0F);
            if (len < 1) {
                len = 1;
            } else if (len > 10) {
                len = 10;
            }
        }

        List<MotionEvent> res = new ArrayList();

        for(int i = 1; i <= len; ++i) {
            float t = (float)((double)i * 1.0 / (double)len);
            float num1 = (1.0F - t) * (1.0F - t);
            float num2 = 2.0F * (1.0F - t) * t;
            float num3 = t * t;
            float x = mx1 * num1 + this.preEvent2.getX() * num2 + mx2 * num3;
            float y = my1 * num1 + this.preEvent2.getY() * num2 + my2 * num3;
            float p = mp1 * num1 + this.preEvent2.getPressure() * num2 + mp2 * num3;
            res.add(MotionEvent.obtain(this.downTime, System.currentTimeMillis(), event.getAction(), x, y, p, event.getSize(), event.getMetaState(), event.getXPrecision(), event.getYPrecision(), event.getDeviceId(), event.getEdgeFlags()));
        }

        return res;
    }

    private static float distrance(MotionEvent e1, MotionEvent e2) {
        float dx = e1.getX() - e2.getX();
        float dy = e1.getY() - e2.getY();
        return (float)Math.sqrt((double)(dx * dx + dy * dy));
    }

    private static float distrance(float x1, float y1, float x2, float y2) {
        float dx = x1 - x2;
        float dy = y1 - y2;
        return (float)Math.sqrt((double)(dx * dx + dy * dy));
    }
}
